<?php namespace App\Http\Controllers\user\uc\devicetech;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Input;
use Validator;
use App\Models\Technologies;
use App\Models\Techs;
use App\Models\Domains;
use App\Models\TechDomain;
use App\Models\TechTech;
use App\Models\MainBody;
use App\Models\TechMainBody;
use App;
use Illuminate\Pagination\LengthAwarePaginator;
use Landish\Pagination\Pagination as BootstrapPresenter;

class TechController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		$uid = $this->getUid();
		$model = Technologies::where("uid", $uid);
		if($model->count())
		{
			$numPerPage = 20;
			$count = $model->count();

			$curPage = (int)Input::get("page");
			if(!$curPage){
				$curPage = 1;
			}
			$p = new LengthAwarePaginator($model, $count, $numPerPage, $curPage);
			$p->setPath("");
	        $presenter = new BootstrapPresenter($p);
	        $data = $model->skip(($curPage - 1) * $numPerPage)->take($numPerPage)->get()->toArray();
		}
		else
		{
			$data = [];
			$presenter = [];
		}
		return view('user.uc.devicetech.tech', ["data" => $data])->withPresenter($presenter)->withIsTech(true);
	}

	public function editTech()
	{
		$techs = Techs::all()->toArray();
		$domains = Domains::where("parent_id", 0)->get()->toArray();
		$mBody = MainBody::all()->toArray();
		return view('user.uc.devicetech.edit_tech', ["techs" => $techs, "domains" => $domains, "mBody" => $mBody])->withIsTech(true);
	}

	public function techDetail()
	{
		return view('user.uc.devicetech.tech_detail')->withIsTech(true);
	}


	public function addTech()
	{
		$rule = [];
		$msg = [];
		$validator = Validator::make(Input::all(), $rule, $msg);
		if($validator->passes())
		{
			$uid = $this->getUid();
			$imgArr = Input::only("image");
			$this->moveImages($imgArr, env("TECH_DIR"));
			$data = Input::all();
			$data["uid"] = $uid;
			$tech = Technologies::create($data);
			$techTech = [
				"cate_id"	=>	Input::get("tech_id"),
				"tech_id"	=>	$tech->id
			];
			TechTech::create($techTech);
			$techDomain = ["cate_id" => Input::get("domain_id"), "tech_id" => $tech->id];
			TechDomain::create($techDomain);
			$device_main = Input::get("main_body");
			foreach ($device_main as $main)
			{
				TechMainBody::create(["mb_id" => $main, "tech_id" => $tech->id]);
			}

			return $this->json([]);
		}
		else
		{
			$errors = $validator->messages();
			return self::error('',$errors->first());
		}
	}

	public function updateTech()
	{
		$rule = [];
		$msg = [];
		$validator = Validator::make(Input::all(), $rule, $msg);
		if($validator->passes())
		{
			$tid = Input::get("tid");
			$uid = $this->getUid();
			$model = Technologies::where("id", $tid)->where("uid", $uid);
			if($model->count())
			{
				$data = Input::except("tid", "tech_id", "domain_id", "main_body");
				$imgArr = Input::only("image");
				$this->moveImages($imgArr, env("TECH_DIR"));
				$model->update($data);
				$techTech = TechTech::where("tech_id", $tid);
				$techTech->update(["cate_id" => Input::get("tech_id")]);
				$domain = TechDomain::where("tech_id", $tid);
				$domain->update(["cate_id" => Input::get("domain_id")]);
				$main_model = TechMainBody::where("tech_id", $tid);
				$main_model->delete();
				$device_main = Input::get("main_body");
				foreach ($device_main as $main)
				{
					TechMainBody::create(["mb_id" => $main, "tech_id" => $tid]);
				}

				return $this->json([]);
			}
		}
		else
		{
			$errors = $validator->messages();
			return self::error('',$errors->first());
		}
	}

	public function getTech()
	{
		$tid = (int)Input::get("tid");
		$uid = $this->getUid();
		$model = Technologies::with(['techDomain', 'techTech', 'techDomain.domain', 'mainbody'])->where("id", $tid);
		if($model->count() && $model->get()[0]->uid == $uid){
			$data = $model->get()[0]->toArray();
			return $this->json($data);
		}
	}

	public function techShow()
	{
		$tid = Input::get("tid");
		$model = Technologies::where("id", $tid);
		if($model->count()){
			$verify_state = $model->pluck("verify_state");
			if($verify_state == 0){
				return self::error(self::IS_AUDITING);
			}else if($verify_state == 2){
				return self::error(self::AUDIT_NOT_PASS);
			}else{
				return $this->json([]);
			}
		}
	}


	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}
}
